package com.wolfsea.windowmanagerdemo

import android.graphics.PixelFormat
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import kotlinx.android.synthetic.main.popwindow_set_wallpaper.view.*

/**
 *@desc  WindowView构建者
 *@author liuliheng
 *@time 2022/4/17  0:24
 **/
class WindowViewBuilder {

    private lateinit var windowView: WindowView

    private var windowManager: WindowManager? = null

    fun show() {
        windowManager?.also {
            try {
                it.addView(windowView, initLayoutParam())
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        windowView.ll_popup_content_layout.setOnClickListener {
            windowView.hide()
        }
    }

    fun onDestroy(activity: AppCompatActivity) {
        windowView.hide(true)

        if (windowViewMap.containsKey(activity.toString())) {
            windowViewMap.remove(activity.toString())
        }
    }

    private fun initLayoutParam(): WindowManager.LayoutParams {
        val layoutParam = WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            0, 0,
            PixelFormat.TRANSPARENT
        )

        layoutParam.height = WindowManager.LayoutParams.MATCH_PARENT
        layoutParam.gravity = Gravity.TOP
        layoutParam.flags =
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or //不获取焦点,以便于在弹出界面的时候,下层界面任然可以进行操作
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                    WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR or //确保内容不会被装饰物(如状态栏)覆盖
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM //确保输入法弹出的时候不会被挤压到状态栏下面

        //popup window的层级为TYPE_APPLICATION_SUB_PANEL
        layoutParam.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL
        return layoutParam
    }

    private fun setActivity(activity: AppCompatActivity) {
        windowView = WindowView(activity)
        windowManager = activity.windowManager
    }

    companion object {

        private val windowViewMap = mutableMapOf<String, WindowViewBuilder>()

        fun create(activity: AppCompatActivity): WindowViewBuilder {
            val builder = WindowViewBuilder()
            builder.setActivity(activity)
            Handler(Looper.getMainLooper()).post {
                windowViewMap[activity.toString()]?.windowView?.let {
                    if (it.isAttachedToWindow) {
                        ViewCompat.animate(it).alpha(0F).withEndAction {
                            if (it.isAttachedToWindow) {
                                activity.windowManager.removeViewImmediate(it)
                            }
                        }
                    }
                }
                windowViewMap[activity.toString()] = builder
            }
            return builder
        }
    }

}